<?php
/**
 * RoomController
 *
 */
class RoomController extends Zend_Controller_Action 
{
    protected $model;
    protected $setpoint_model;
    protected $setting_model;
    protected $room_zone_model;
 
    protected function _getModel()
    {
        if (null === $this->model) {
            require_once APPLICATION_PATH . '/models/Room.php';
            $this->model = new Model_Room();
        }
        return $this->model;
    } 
    
    protected function _getSetpointModel()
    {
        if (null === $this->setpoint_model) {
            require_once APPLICATION_PATH . '/models/Setpoint.php';
            $this->setpoint_model = new Model_Setpoint();
        }
        return $this->setpoint_model;
    }   
     
    protected function _getSettingModel() 
    {
      if (null === $this->setting_model) {
          require_once APPLICATION_PATH . '/models/Setting.php';
          $this->setting_model = new Model_Setting();
      }
      return $this->setting_model;
    }

    protected function _getRoomZoneModel() 
    {
      if (null === $this->room_zone_model) {
          require_once APPLICATION_PATH . '/models/RoomZone.php';
          $this->room_zone_model = new Model_RoomZone();
      }
      return $this->room_zone_model;
    }

    protected function _getMoteGatewayModel()
    {
        if (null === $this->mote_gateway) {
            require_once APPLICATION_PATH . '/models/MoteGateway.php';
            $this->mote_gateway = new Model_MoteGateway();
        }
        return $this->mote_gateway;
    }
    
    protected function getAddForm($scale)
    {
        require_once APPLICATION_PATH . '/forms/Room.php';
        $form = new Form_Room();
        $form->getAddForm($scale);
        return $form;
    }

    protected function getEditForm($scale)
    {
        require_once APPLICATION_PATH . '/forms/Room.php';
        $form = new Form_Room();
        $form->getEditForm($scale);
        return $form;
    }

    
    public function indexAction() 
    {
          return $this->_helper->redirector->gotoSimple('add','room'); 
    //      $this->addAction();
    }  
    
    
    protected function scaleSetpoint($setpoint, $scale) {
      if ($scale == "C") { // only save F in DB
        require_once APPLICATION_PATH . '/models/Reading.php';
        return Model_Reading::toFahrenheit($setpoint);
      }
      return $setpoint;
    }
    
    public function editAction()
    {
      $request = $this->getRequest();
      $scale  = ($this->_getSettingModel()->isCelsius() == true) ? "C":"F";
      $form    = $this->getEditForm($scale);
      
      if ($this->getRequest()->isPost()) {
          if ($form->isValid($request->getPost())) {
            $model      = $this->_getModel();
            $zone_model = $this->_getRoomZoneModel();
            $sp_model   = $this->_getSetpointModel();
            $mote_gateway = $this->_getMoteGatewayModel();  
            
            $values     = $form->getValues();

            $room_name = $values['name'];
            $zone_id   = $values['zone_id'];
            $setpoint  = $this->scaleSetpoint($values['setpoint'], $scale);
            $room_id   = $values['room_id'];
            // room
            $room_data['name']    = $room_name;
            $room_data['room_id'] = $room_id;
            $model->update($room_data);
            // zone 
            $zone_data['room_id'] = $room_id;
            $zone_data['zone_id'] = $zone_id;
            $zone_model->update($zone_data);
            // setpoint 
            $sp_data['room_id']  = $room_id;
            $sp_data['setpoint'] = $setpoint;
            $sp_model->update($sp_data);   
            
            // Update Motes
            $mote_gateway->updateSetpoint($room_id, $setpoint);      
            //exit;    
            
            return $this->_helper->redirector->gotoSimple('index','index');
          }
      }      

      $room_id = $request->getParam("room_id");

      $rm_model = $this->_getModel();
      $sp_model = $this->_getSetpointModel();
      $zm_model = $this->_getRoomZoneModel();      
      
      $sp       = $sp_model->fetchEntry($room_id);
      $zm       = $zm_model->fetchEntry($room_id);
      $rm       = $rm_model->fetchEntry($room_id);
      if ($scale == "C") { // only save F in DB
        require_once APPLICATION_PATH . '/models/Reading.php';
        $sp['setpoint']  = round(Model_Reading::toCelsius($sp['setpoint']), Model_Reading::getPrecision());
      }
      
      $data = array('setpoint'=>$sp['setpoint'],
                    'room_id' =>$rm['room_id'],
                    'name'    =>$rm['name'],
                    'zone_id' =>$zm[0] 
                   );                   
      $form->populate($data);
      
      $this->view->form = $form;
      $this->view->nav = "index";
      $this->view->scale  = $scale;
    }
    
    public function addAction() 
    {
      $request = $this->getRequest();
      $scale  = ($this->_getSettingModel()->isCelsius() == true) ? "C":"F";
      $form    = $this->getAddForm($scale);

      if ($this->getRequest()->isPost()) {
          if ($form->isValid($request->getPost())) {
            $model      = $this->_getModel();
            $zone_model = $this->_getRoomZoneModel();
            $sp_model   = $this->_getSetpointModel();
            $mote_gateway = $this->_getMoteGatewayModel();          
            
            $values     = $form->getValues();

            $room_name = $values['name'];
            $zone_id   = $values['zone_id'];
            $setpoint  = $this->scaleSetpoint($values['setpoint'], $scale);
            // room
            $room_data['name'] = $room_name;
            $room_id = $model->save($room_data);
            
            // zone 
            $zone_data['room_id'] = $room_id;
            $zone_data['zone_id'] = $zone_id;
            $zone_model->save($zone_data);
            // setpoint 
            $sp_data['room_id']   = $room_id;
            $sp_data['setpoint']  = $setpoint; 
            $sp_model->save($sp_data);  
            
            // update Motes
            $mote_gateway->updateSetpoint($room_id, $setpoint); 

            return $this->_helper->redirector->gotoSimple('index','index');
          }
        }
        $this->view->form = $form;
        $this->view->nav = "room";
        $this->view->scale  = ($this->_getSettingModel()->isCelsius() == true) ?  "C"    : "F";
      }
}
